package com.ym.jiujiangeyou.app;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import cn.sharesdk.framework.ShareSDK;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;
import com.ym.jiujiangeyou.util.FileManager;
import com.ym.jiujiangeyou.util.NetworkUtil;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.widget.Toast;

/**
 * @author 叶铭
 * @email yeming_1001@163.com
 * @version V1.0
 */
public class MyApplication extends Application {

	private static MyApplication instance;
	//private List<Activity> activityList = new LinkedList<Activity>();
	public static int mNetWorkState;
	public boolean m_bKeyRight = true;
	public BMapManager mBMapManager = null;
	public static String cacheSize = null ;
	
	public List<String> urls = null ;  //图片链接
	
	public Map<String,List<String>> sceneryUrlsCache = new HashMap<String, List<String>>() ;
	public Map<String,List<String>> hotelUrlsCache = new HashMap<String, List<String>>() ;
	
	public void setSceneryUrlsCache(String key,List<String> urls){
		if(key == null || urls.isEmpty()){
			return ;
		}
		System.out.println("--------------------si----"+urls.size()) ;
		sceneryUrlsCache.put(key, urls) ;
	}
	
	public  List<String> getSceneryUrlsCache(String key){
		if(key == null){
			return null;
		}
		return sceneryUrlsCache.get(key) ;
		
	}
	
	public  void setHotelUrlsCache(String key,List<String> urls){
		if(key == null || urls.isEmpty()){
			return ;
		}
		hotelUrlsCache.put(key, urls) ;
	}
	
	public  List<String> getHotelUrlsCache(String key){
		if(key == null){
			return null;
		}
		return hotelUrlsCache.get(key) ;
		
	}

	public List<String> getUrls() {
		return this.urls;
	}

	public void setUrls(List<String> urls) {
		this.urls = urls;
	}

	public MyApplication() {
	}

	public static synchronized MyApplication getInstance() {
		if (instance == null) {
			instance = new MyApplication();
		}
		return instance;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		instance = this;
		mNetWorkState = NetworkUtil.getNetworkState(this);
		initEngineManager(this);
		ShareSDK.initSDK(this);  //初始化 sharesdk
		/*new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				cacheSize = getCacheSize() ;
			}
		}).start() ;*/
	}

	public void initEngineManager(Context context) {
		if (mBMapManager == null) {
			mBMapManager = new BMapManager(context);
		}

		if (!mBMapManager.init(new MyGeneralListener())) {
			Toast.makeText(MyApplication.getInstance().getApplicationContext(),
					"BMapManager  初始化错误!", Toast.LENGTH_LONG).show();
		}
	}

	

	// 常用事件监听，用来处理通常的网络错误，授权验证错误等
	public static class MyGeneralListener implements MKGeneralListener {

		@Override
		public void onGetNetworkState(int iError) {
			if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
				Toast.makeText(
						MyApplication.getInstance().getApplicationContext(),
						"您的网络出错啦！", Toast.LENGTH_LONG).show();
			} else if (iError == MKEvent.ERROR_NETWORK_DATA) {
				Toast.makeText(
						MyApplication.getInstance().getApplicationContext(),
						"输入正确的检索条件！", Toast.LENGTH_LONG).show();
			}
			// ...
		}

		@Override
		public void onGetPermissionState(int iError) {
			// 非零值表示key验证未通过
			if (iError != 0) {
				// 授权Key错误：
				Toast.makeText(
						MyApplication.getInstance().getApplicationContext(),
						"请检查您的网络连接是否正常！error: "
								+ iError, Toast.LENGTH_SHORT).show();
				MyApplication.getInstance().m_bKeyRight = false;
			} else {
				MyApplication.getInstance().m_bKeyRight = true;
				Toast.makeText(
						MyApplication.getInstance().getApplicationContext(),
						"key认证成功", Toast.LENGTH_LONG).show();
			}
		}
	}

	/**
	 * 
	 * @retur void
	 * @Description: 删除数据库缓存
	 */
	public void clearAppCache() {
		deleteDatabase("collect.db");
		deleteDatabase("collect.db-shm");
		deleteDatabase("collect.db-wal");
	}

	/**
	 * @return
	 * @retur String
	 * @Description: 计算缓存大小
	 */
	public String getCacheSize() {
		long fileSize = 0;
		String cacheSize = "0KB";
		File filesDir = getFilesDir();
		File cacheDir = getCacheDir();
		/*File file = new File(FileManager.getSaveFilePath());
		if (file.exists()) {
			fileSize += FileManager.getDirSize(file);
		}*/
		fileSize += FileManager.getDirSize(filesDir);
		fileSize += FileManager.getDirSize(cacheDir);

		if (fileSize > 0)
			cacheSize = FileManager.formatFileSize(fileSize);
		return cacheSize;
	}
}
